#!/bin/bash

PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin'

# 自定义变量
log='./service.log'
my_mail='544025211@qq.com'
title='Jumpserver故障'

# 进入脚本所在路径
base_dir="$(cd "$(dirname $0)"; pwd)"
cd $base_dir

# 检测的服务
service_list='jms coco docker nginx'

# 重启服务
function restart_jumpserver(){
    stop_list='nginx docker coco jms'
    for service in $stop_list; do
        systemctl stop $service
        if test $? -ne 0; then
            echo "$(date '+%Y-%m-%d %H:%M:%S') --> [INFO]: Stop $service Failed!" | tee -a $log
        else
            echo "$(date '+%Y-%m-%d %H:%M:%S') --> [INFO]: Stop $service Successfully!" | tee -a $log
        fi
    done
    sleep 5

    start_list='jms coco nginx docker'
    for service in $start_list; do
        [ "$service" == 'docker' ] && sleep 10
        systemctl start $service
        if test $? -ne 0; then
            echo "$(date '+%Y-%m-%d %H:%M:%S') --> [INFO]: Start $service Failed!" | tee -a $log
        else
            echo "$(date '+%Y-%m-%d %H:%M:%S') --> [INFO]: Start $service Successfully!" | tee -a $log
        fi
    done
    sleep 10
}

# 检测服务状态
function check_service(){
    service=$1
    echo "$(date '+%Y-%m-%d %H:%M:%S') ----> [CHECK]: $service status"
    systemctl status $service >& /dev/null
    if test $? -ne 0; then
        echo "$(date '+%Y-%m-%d %H:%M:%S') --> [ERROR]: $service is down!" | tee -a $log
        let num++
        restart_jumpserver
    else
        echo "$(date '+%Y-%m-%d %H:%M:%S') --> [INFO]: $service is running!" | tee -a $log
    fi
}

# 循环执行3次
for ((i=0;i<=3;i++)); do
    for service in $service_list; do
        num=0
        check_service $service
        # 如果检测一次通过则成功
        if test $? -eq 0; then
            i=4
        fi
    done
    sleep 60
done

# 发送邮件
for ((i=0;i<=3;i++)); do
    cat $log | grep 'ERROR' >& /dev/null
    if test $? -eq 0; then
        ./send.py "$my_mail" "$title" "$(cat $log)"
        if test $? -ne 0; then
            echo "$(date '+%Y-%m-%d %H:%M:%S') --> [ERROR]: Email sent Failed!" >> ./mail.log
        else
            echo "$(date '+%Y-%m-%d %H:%M:%S') --> [INFO]: Email sent Fuccessfully!" >> ./mail.log
            i=3
        fi
    fi
done

mv $log logs/${log}.$(date +%Y%m%d%H%M)
